//
//  CMDefine.m
//  ThridPackage
//
//  Created by 韩艳锋 on 2019/7/22.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "CMDefine.h"
#import <objc/runtime.h>
//@implementation CMDefine
//
//@end
@interface NSObject ()

@end

@protocol TextPtotocol <NSObject>

-(void)methodRequare;

@optional

-(void)methodOptional;
@end
@implementation NSObject (HFProtocolDefoutImplement)


//+ (BOOL)resolveInstanceMethod:(SEL)sel {
//
//    NSString * selctor = NSStringFromSelector(sel);
//    unsigned protocolCount;
//    Protocol * __unsafe_unretained *protocols = class_copyProtocolList([self class], &protocolCount);
//    //第二层遍历类中所有的协议
//    for (int i = 0; i != protocolCount; i++) {
//        Protocol *protocol = protocols[i];
//        for (int k = 0; k != 2; k ++) {
//            unsigned methodCount;
//            struct objc_method_description * method_description = protocol_copyMethodDescriptionList(protocol, k, YES, &methodCount);
//            for (int j = 0; j != methodCount; j++) {
//                struct objc_method_description des = method_description[j];
//                if ([NSStringFromSelector(des.name) isEqualToString:selctor]) {
//                    NSString *tempClassName = [NSString stringWithFormat:@"%s_HFDefoutImplementation", protocol_getName(protocol)];
//                    Class tempClass = objc_getClass(tempClassName.UTF8String);
//                    if (!tempClass) continue;
//                    Method _Nullable oldMethod = class_getInstanceMethod(tempClass, sel);
//                    IMP imp = method_getImplementation(oldMethod);
//                    SEL method_name = method_getName(oldMethod);
//                    const char * _Nullable method_types = method_getTypeEncoding(oldMethod);
//                    BOOL isSussed = class_addMethod([self class], method_name, imp, method_types);
//                    free(protocols);
//                    return isSussed;
//                }
//            }
//            free(method_description);
//        }
//    }
//    free(protocols);
//    return NO;
//}

@end

//@extensionProtocol(UIViewControllerPeo)
//- (void)antionOne {
//    NSLog(@"%@",@"UIViewController_HFDefoutImplementation _ antionOne");
//
//}
//- (void)actionTow {
//    NSLog(@"%@",@"UIViewController_HFDefoutImplementation _ antionTow");
//}
//@end

//
//__attribute__((constructor)) static void _append_default_implement_method_to_class() {
//    NSLog(@"%@",@"开始");
//    unsigned classCount;
//    Class *classes = objc_copyClassList(&classCount);
//    //第一层遍历所有的类
//    for (int i = 0; i < classCount; i ++) {
//        Class class = classes[i];
//        Class metaClass = object_getClass(class);
//
//        unsigned protocolCount;
//        Protocol * __unsafe_unretained *protocols = class_copyProtocolList(class, &protocolCount);
//        //第二层遍历类中所有的协议
//        for (int j = 0; j < protocolCount; j ++) {
//            Protocol *protocol = protocols[j];
//            NSString *tempClassName = [NSString stringWithFormat:@"%s_HFDefoutImplementation", protocol_getName(protocol)];
//            Class tempClass = objc_getClass(tempClassName.UTF8String);
//            if (!tempClass) continue;
//
//            unsigned methodCount;
//            Method *methods = class_copyMethodList(tempClass, &methodCount);
//            //第三层遍历临时类的所有方法并添加
//            for (int k = 0; k < methodCount; k ++) {
//                Method method = methods[k];
//                class_addMethod(class, method_getName(method), method_getImplementation(method), method_getTypeEncoding(method));
//            }
//            free(methods);
//
//            Class metaTempClass = object_getClass(tempClass);
//            unsigned metaMethodCount;
//            Method *metaMethods = class_copyMethodList(metaTempClass, &metaMethodCount);
//            //第三层遍历临时类元类的所有方法并添加
//            for (int k = 0; k < metaMethodCount; k ++) {
//                Method method = metaMethods[k];
//                class_addMethod(metaClass, method_getName(method), method_getImplementation(method), method_getTypeEncoding(method));
//            }
//            free(metaMethods);
//        }
//        free(protocols);
//    }
//    free(classes);
//    NSLog(@"%@",@"结束");
//}
